package com.atu.poker.service;

import java.util.List;

import com.atu.poker.dao.RoomInfoDAO;
import com.atu.poker.dao.RoomSeatDAO;
import com.atu.poker.dao.UserInfoDAO;
import com.atu.poker.dao.impl.RoomInfoDAOJdbcImpl;
import com.atu.poker.dao.impl.RoomSeatDAOJdbcImpl;
import com.atu.poker.dao.impl.UserInfoDAOJdbcImpl;
import com.atu.poker.po.RoomInfo;
import com.atu.poker.po.Seat;
import com.atu.poker.util.DateTimeUtil;
import com.atu.poker.util.RespUtil;
import com.atu.poker.util.UserUtil;

public class RoomInfoService {

	private RoomInfoDAO roomInfoDAO = new RoomInfoDAOJdbcImpl();
	private RoomSeatDAO roomSeatDAO = new RoomSeatDAOJdbcImpl();
	private UserInfoDAO userInfoDAO = new UserInfoDAOJdbcImpl();

	public Object createRoom(String userId, String customName, String upperChip, String lowerChip, String userLimit) {
		if (roomInfoDAO.getNotCloseRoomStatus(userId) == null) {

			RoomInfo roomInfo = new RoomInfo();
			String roomId = UserUtil.getUUID();
			String roomName = "【" + userInfoDAO.getUserName(userId) + "】的房间-房间号【" + roomId + "】";

			roomInfo.setRoomId(roomId);
			roomInfo.setRoomName(roomName);
			roomInfo.setCreateUser(userId);
			roomInfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
			roomInfo.setUpperChip(upperChip);
			roomInfo.setLowerChip(lowerChip);
			roomInfo.setUserLimit(userLimit);
			roomInfo.setRoomStatus("1");
			roomInfoDAO.createRoom(roomInfo);
			String seatId = UserUtil.getUUID();
			// MainSocket.seatMap.map.put(seatId, outputStream);
			roomSeatDAO.insertOwnerUser(seatId, roomId, userId, "0");

			// 将自己的座位信息发送到自己的客户端
			Seat selfSeat = roomSeatDAO.getSelfSeat(seatId);
			// 创建房间成功601
			return RespUtil.respTypeMessage("601", userId, "selfSeat", selfSeat);
		} else {
			// 创建房间失败，已经有创建完成未关闭房间602
			return RespUtil.respTypeCode("602");
		}
	}

	public Object getNotCloseRoomList(String filterRoom) {
		List<RoomInfo> roomList = roomInfoDAO.getNotCloseRoomList(filterRoom);
		// 获取所有未关闭的房间701
		return RespUtil.respTypeMessage("701", "", "roomList", roomList);
	}

	public void closeRoom(String seatId) {
		// 如果该seatId是房主的话，退出则关闭房间
		if ("0".equals(roomSeatDAO.getSeatNo(seatId))) {
			roomInfoDAO.changeRoomStatus(seatId, "0");
		}
	}

	public Object joninRoom(String roomId, String userId) {
		long seatNo = roomSeatDAO.userCount(roomId);
		long userLimit = Long.parseLong(roomInfoDAO.getUserLimit(roomId));

		//如果用户还没有加入房间
		if (roomSeatDAO.alreadyJoinRoom(userId) == 0) {

			// 判断房间是否还可以加入玩家，房间座位满的话不能加入该坊间
			if (seatNo < userLimit) {
				String seatId = UserUtil.getUUID();
				// MainSocket.seatMap.map.put(seatId, outputStream);
				roomSeatDAO.insertOwnerUser(seatId, roomId, userId, seatNo + "");
				/*
				 * //如果该加入玩家是该房间最后一个加入的（根据userLimit判断） if (seatNo+1==userLimit) { String
				 * bankerSeatId = roomSeatDAO.getSeatId(roomId, "0"); //改变房间状态，房间已满，正在游戏中
				 * roomInfoDAO.changeRoomStatus(bankerSeatId, "2"); //通知所有玩家，指针的位置指到房主 for (int
				 * i = 0; i < userLimit; i++) { OutputStream everyOutputStream = (OutputStream)
				 * MainSocket.seatMap.map.get(roomSeatDAO.getSeatId(roomId, i+"")); try {
				 * //当前指针指向房主信息发送给每一个玩家 //因为该房间玩家以齐，所以获取房主输出流，通知房主可以发牌
				 * everyOutputStream.write((RespUtil.respTypeCode("001")+"\n").getBytes("utf-8")
				 * ); } catch (Exception e) { e.printStackTrace(); } }
				 * 
				 * OutputStream bankerOutputStream = (OutputStream)
				 * MainSocket.seatMap.map.get(bankerSeatId); try { //向房主发送可以发牌指令001
				 * bankerOutputStream.write((RespUtil.respTypeCode("001")+"\n").getBytes("utf-8"
				 * )); } catch (Exception e) { e.printStackTrace(); } }
				 */
				// 将自己的座位信息发送到自己的客户端
				Seat selfSeat = roomSeatDAO.getSelfSeat(seatId);
				// 加入房间成功801
				return RespUtil.respTypeMessage("801", userId, "selfSeat", selfSeat);
			} else {
				// 加入房间失败，房间已满802
				return RespUtil.respTypeCode("802");
			}
		} else {
			//加入房间失败，用户已经加入房间803，客户端还未做处理
			return RespUtil.respTypeCode("803");
		}
	}
}
